<?php
/*==============================================/* 
*| DuyK - DC-Team
*| Class gio hang
/*==============================================*/

	if(!class_exists('class_manage'))
		{
		exit('Nhin gi ha');
		}
		
	class class_giohang extends class_manage
		{
		
		/*==============================================/* 
		*| Function them hang vao gio, function them_hang()
		*| Input: ID cua san pham, so luong san pham
		*| Tao ra $_SESSION['item']
		*| Gia tri la 1 string mang thong tin dat hang
		*| Language key su dung: them_hang
		/*==============================================*/
			function them_hang($item, $soluong)
				{
					// Input filter
					$item = intval($item);
					$soluong = intval($soluong);
					
					// Create SESSION
					if( !preg_match('/\|'.$item.'\-/', $_SESSION['item']) )
					{
					$_SESSION['item'] .= '|'.$item.'-'.$soluong;
					
					// Lay thong tin san pham tu class_sanpham
					$sanpham = $this->load('class_sanpham')->xem_san_pham($item);
					
					// Them thong tin so luong vao array $sanpham
					$sanpham['soluong'] = $soluong;
					}
					// Tra ve ket qua kem language key them_hang
					return $this->language_replace('them_hang', $sanpham);
				}
				
		/*==============================================/* 
		*| Function xem gio hang, function xem_gio_hang
		*| Input: $page: trang dang xem, $result: ket qua/trang
		*| Lay du lieu tu $_SESSION['item'] de truy van DB
		*| Tra ve 1 array lon', cac value la array mang thong tin tung san pham
		/*==============================================*/		
			function xem_gio_hang($page=1, $result=10)
				{
					// input filter
					$page = intval($page);
					$result = intval($result);
						$dautrang = $page*$result-$result;
					$_SESSION['item'] = $this->data->escapeString($_SESSION['item']);
					
					// Lay thong tin tu SESSION item
					$item = explode('|', $_SESSION['item']);
					
					// Output variable
					$return = array();
					
						foreach($item as $key => $value)
							{	
								// Phan trang
								if($key>=$dautrang and $key<=($dautrang+$result) )
								{
									$value_array = explode('-', $value);
									
									// Load info tu class_sanpham
									$item_info = $this->load('class_sanpham')->xem_san_pham($value_array[0]);
									
									// Add thong tin so luong
									$item_info['soluong'] = $value_array[1];
									
									array_push($return, $item_info);
								}
								
								// Break
								if($key>= ($dautrang+$result) )
									{
									break;
									}
							}
					return $return;
				}
				
		/*==============================================/* 
		*| Ham cap nhat dung de cap nhat so luong hoac xoa item
		*| Neu $soluong = 0 => Xoa item
		*| Function cap_nhat_gio_hang($item, $soluong)
		*| Language key su dung: cap_nhat_gio_hang
		/*==============================================*/
			function cap_nhat_gio_hang($item, $soluong)
				{
					$soluong_cu = explode('|'.$item.'-', $_SESSION['item']);
					$soluong_cu = explode('|', $soluong_cu[1]);
					$soluong_cu = $soluong_cu[0];
					if($soluong>0)
						{
						$_SESSION['item'] = str_replace('|'.$item.'-'.$soluong_cu, '|'.$item.'-'.$soluong, $_SESSION['item']);
						}
					if($soluong==0)
						{
						$_SESSION['item'] = str_replace('|'.$item.'-'.$soluong_cu, '', $_SESSION['item']);
						}
					
					return $this->language_normal('cap_nhat_gio_hang');
				}
				
		/*==============================================/* 
		*| Ham checkout(), dung de thanh toan
		*| input: NONE
		*| Tra ve: array la list info va tong gia tri thanh toan
		*| Key: 'tong_tien' => Tong gia tri cua cac mat hang
		/*==============================================*/		
			function checkout()
				{
					$list_hang = $this->xem_gio_hang(1,999);
					foreach($list_hang as $sanpham)
						{
							$tong_tien = $tong_tien + $sanpham['soluong']*$sanpham['gia'];
						}
					// Build output
					$return = array();
					$return = $list_hang;
					$return['tong_tien'] = $tong_tien;
					
					// Return
					return $return;
				}
		
		}

?>